using System;
using System.IO;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ValidationFramework.Web.ASP
{
    internal class ExtendedRequiredFieldValidator : BaseValidator
    {
        #region Fields

        private static readonly Type extendedRequiredFieldValidatorType = typeof (ExtendedRequiredFieldValidator);
        private bool trimWhiteSpace;

        #endregion


        #region Methods

		/// <inheritdoc />		
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (RenderUplevel)
            {
                if (!Page.ClientScript.IsClientScriptBlockRegistered(extendedRequiredFieldValidatorType, "ExtendedRequiredFieldValidatorMethod"))
                {
                    using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ValidationFramework.Web.WebValidators.Validation.js"))
                    {
                        using (var streamReader = new StreamReader(stream))
                        {
                            var script = streamReader.ReadToEnd();
                            Page.ClientScript.RegisterClientScriptBlock(extendedRequiredFieldValidatorType, "ExtendedRequiredFieldValidatorMethod", script);
                        }
                    }
                }
            }
        }


		/// <inheritdoc />	
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);

            if (RenderUplevel)
            {
                //this attribute is needed by the RequiredFieldValidator's validation method.
                Page.ClientScript.RegisterExpandoAttribute(base.ClientID, "initialValue", InitialValue);
                //this is the method that asp.net calls when validating on the client-side
                Page.ClientScript.RegisterExpandoAttribute(base.ClientID, "evaluationfunction", "RequiredValidatorEvaluateIsValid2");
                Page.ClientScript.RegisterExpandoAttribute(base.ClientID, "trimWhiteSpace", TrimWhiteSpace.ToString());
                Page.ClientScript.RegisterExpandoAttribute(base.ClientID, "ignoreCase", IgnoreCase.ToString());
            }
        }


		/// <inheritdoc />
        /// <returns><see langword="true"/></returns>
        protected override bool EvaluateIsValid()
        {
            return true;
        }

        #endregion


        #region Properties

        /// <summary>
        /// This is the equivalent of the RequiredFieldValidator's InitialValue
        /// property.
        /// </summary>
        public string InitialValue
        {
            get
            {
                var temp = (string) ViewState["InitialValue"];

                if (temp == null)
                {
                    return string.Empty;
                }
                else
                {
                    return temp;
                }
            }
            set
            {
                ViewState["InitialValue"] = value;
            }
        }

        public bool TrimWhiteSpace
        {
            get
            {
                var viewStateTrimWhiteSpace = ViewState["TrimWhiteSpace"];
                if (viewStateTrimWhiteSpace == null)
                {
                    return trimWhiteSpace;
                }
                else
                {
                    return (bool) viewStateTrimWhiteSpace;
                }
            }
            set
            {
                trimWhiteSpace = value;
                ViewState["TrimWhiteSpace"] = value;
            }
        }

        public bool? IgnoreCase
        {
            get
            {
                return (bool?) ViewState["IgnoreCase"];
            }
            set
            {
                ViewState["IgnoreCase"] = value;
            }
        }

        #endregion
    }
}